package com.aaa.ssm.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 在处理业务之前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("在业务执行前，执行----------------");
        //获取session中的userInfo
        Object userInfo = request.getSession().getAttribute("userInfo");
        //判断是否为null
        if(userInfo==null){//说明没有登录
            //重定向到登录页面
            response.sendRedirect("/sysuser/toLogin");
            //返回false中断执行
            return false;
        }
        //返回true继续执行
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("在业务执行时，执行-------------");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("在业务执行后，执行-------------");
    }
}
